/*
 * FileName：VoteRecordServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.util.AjaxResult;
import com.smart.common.util.Util;
import com.smart.wx.wxcms.dao.VoteApplyMapper;
import com.smart.wx.wxcms.dao.VoteRecordMapper;
import com.smart.wx.wxcms.entity.VoteApply;
import com.smart.wx.wxcms.entity.VoteRecord;
import com.smart.wx.wxcms.service.VoteRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 投票记录表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-11 20:30:24
 */
@Service
public class VoteRecordServiceImpl implements VoteRecordService {

	@Resource
	private VoteRecordMapper voteRecordMapper;

	@Resource
	private VoteApplyMapper voteApplyMapper;


    @Override
    public VoteRecord queryObject(VoteRecord voteRecord) throws Exception {
        return voteRecordMapper.queryObject(voteRecord);
    }
	
	@Override
	public List<VoteRecord> queryList(VoteRecord voteRecord) throws Exception {
		return voteRecordMapper.queryList(voteRecord);
	}

    @Override
    public List<VoteRecord> queryListByPage(VoteRecord voteRecord) throws Exception {
        return voteRecordMapper.queryListByPage(voteRecord);
    }

	@Override
	public Integer queryCount(VoteRecord voteRecord) throws Exception {
        Integer ct = voteRecordMapper.queryCount(voteRecord);
        return ct == null ? 0 : ct;
	}
	
	@Override
	public Integer insert(VoteRecord voteRecord) throws Exception {
        return voteRecordMapper.insert(voteRecord);
	}
	
	@Override
	public Integer update(VoteRecord voteRecord) throws Exception {
        return voteRecordMapper.update(voteRecord);
	}

	@Override
	public Integer delete(VoteRecord voteRecord) throws Exception {
		return voteRecordMapper.delete(voteRecord);
	}

	@Override
	public Integer deleteBatch(List<String> ids) throws Exception {
		return voteRecordMapper.deleteBatch(ids);
	}


	@Override
	public AjaxResult save(VoteRecord voteRecord) throws Exception {

    	String openId = voteRecord.getOpenId();

    	synchronized (this){
			/******* STEP_1：防止直接调用该接口 进行不良刷票 ***********/
			if(openId != null && !openId.equals("")){

				/******* STEP_2：防止重复投票 ***********/
				VoteRecord tempBean = new VoteRecord();
				tempBean.setVoteApplyId(voteRecord.getVoteApplyId());
				tempBean.setOpenId(openId);
				int count = this.voteRecordMapper.queryCount(tempBean);
				if(count > 0){
					return AjaxResult.failure("您已经投过票");
				}

				/******* STEP_3：新增 ***********/

				VoteApply voteApply = new VoteApply();
				voteApply.setId(voteRecord.getVoteApplyId());
				voteApply = this.voteApplyMapper.queryObject(voteApply);

				voteRecord.setAccount(voteApply.getAccount());
				voteRecord.setId(Util.getUUID());
				voteRecord.setCreateTime(new Date());

				voteRecordMapper.insert(voteRecord);
				return AjaxResult.saveSuccess();
			}else{
				return AjaxResult.failure();
			}
		}
	}

	@Override
	public int getVoteCountByVoteId(VoteRecord voteRecord) {
		return voteRecordMapper.getVoteCountByVoteId(voteRecord);
	}

	@Override
	public int getFansCountByVoteId(VoteRecord voteRecord) {
		return this.voteRecordMapper.getFansCountByVoteId(voteRecord);
	}
}
